package com.bit.cfdi.web.validators.perfil;

import org.zkoss.bind.ValidationContext;
import org.zkoss.bind.validator.AbstractValidator;

import com.bit.cfdi.web.domain.Usuario;
import com.bit.cfdi.web.utils.EncryptionUtils;

public class OldPasswordValidator extends AbstractValidator {
	
	private Usuario usuario;
	private EncryptionUtils encryptionUtils;
	private String fieldName;
	
	public OldPasswordValidator(
			final Usuario usuario,
			final EncryptionUtils encryptionUtils,
			final String fieldName) {
		
		this.usuario = usuario;
		this.encryptionUtils = encryptionUtils;
		this.fieldName = fieldName;
	}

	@Override
	public void validate(final ValidationContext ctx) {
		String oldPassword = (String) ctx.getProperties(fieldName)[0]
				.getValue();
		if(oldPassword == null || oldPassword.trim().isEmpty()) {
			return;
		}
		String userPass = usuario.getPassword();
		String encryptedPass = encryptionUtils.stringEncryption(oldPassword);
		if(!encryptedPass.equals(userPass)) {
			addInvalidMessage(ctx, fieldName, "Password Incorrecto");
			return;
		}
	}

}
